using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
using WPFExcelReport.Helpers;
using WPFExcelReport.Controls;
namespace WPFExcelReport
{
    public partial class VCBOOK
    {
        public DataRow Convert2CBOOK(DataRow dwInput, string ITXDBVERSION, bool IsConvert = false)
        {
            DataSet SResult = new DataSet();
            SResult = QueryCBOOK(dwInput, 0);
            DataRow dwReturn = null;
            if (SResult.Tables[0].Rows.Count > 0)
            {
                dwReturn = SResult.Tables[0].Rows[0];
            }
            else
            {
                dwReturn = SResult.Tables[0].NewRow();
            }
            switch (ITXDBVERSION)
            {
                case "YGR":
                    #region=========
         dwReturn["CBMANDANT"] = " ".PadRight(2 ); 
         dwReturn["CBOOKNO"] = " ".PadRight(15 ); 
         dwReturn["CBSTA"] = " ".PadRight(1 ); 
         dwReturn["CBDATE"] = " ".PadRight(0 ); 
         dwReturn["CBZOLLST"] = " ".PadRight(6 ); 
         dwReturn["CBDES1"] = " ".PadRight(50 ); 
         dwReturn["CBDES2"] = " ".PadRight(50 ); 
         dwReturn["CBVALFRM"] = " ".PadRight(0 ); 
         dwReturn["CBVALUPT"] = " ".PadRight(0 ); 
         dwReturn["CBLO"] = " ".PadRight(2 ); 
         dwReturn["CBLB"] = " ".PadRight(2 ); 
         dwReturn["CBBUCHKRS"] = " ".PadRight(3 ); 
         dwReturn["CBVB"] = " ".PadRight(3 ); 
         dwReturn["CBQTY"] = " ".PadRight(0 ); 
         dwReturn["CBME"] = " ".PadRight(3 ); 
         dwReturn["CBMAXVAL"] = " ".PadRight(0 ); 
         dwReturn["CBDAYSEXP"] = " ".PadRight(0 ); 
         dwReturn["CBDAYSCP"] = " ".PadRight(0 ); 
         dwReturn["CBWASTE"] = " ".PadRight(0 ); 
         dwReturn["CBWASTEREA"] = " ".PadRight(1 ); 
         dwReturn["CBWASTEADD"] = " ".PadRight(0 ); 
         dwReturn["CBTRBUCH"] = " ".PadRight(1 ); 
         dwReturn["CBPERLOT"] = " ".PadRight(1 ); 
         dwReturn["CBPERNVE"] = " ".PadRight(1 ); 
         dwReturn["CBEXCRATE1"] = " ".PadRight(1 ); 
         dwReturn["CBEXCRATEID"] = " ".PadRight(1 ); 
         dwReturn["CBFILLER"] = " ".PadRight(61 ); 
         dwReturn["CBAENZL"] = " ".PadRight(0 ); 
         dwReturn["CBAENB"] = " ".PadRight(8 ); 
         dwReturn["CBAEND"] = " ".PadRight(0 ); 
         dwReturn["CBNEUB"] = " ".PadRight(8 ); 
         dwReturn["CBNEUD"] = " ".PadRight(0 ); 
                    #endregion
                    break;
                case "JLT":

                    break;
                default:

                    break;
            }
            if (IsConvert)
            {
                foreach (DataColumn cm in dwInput.Table.Columns)
                {
                    if (dwInput[cm.ColumnName].ToString() == "")
                        continue;
                    if (dwReturn.Table.Columns.Contains(cm.ColumnName))
                    {
                        dwReturn[cm.ColumnName] = dwInput[cm.ColumnName];
                    }
                }
            }
            return dwReturn;
        }
        public void MapCBOOK(ref DataRow dwLocal, DataRow dwData)
        {
            BlindMap(ref dwLocal, dwData);
        }
        public void SaveCBOOK(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            DataSet setResult = new DataSet();
            DataTable tbLocal = null;
            DataRow dwLocal = null;
            bool flgExist = false;
            setResult = QueryCBOOK(dwData);
            if (setResult.Tables.Count > 0)
            {
                tbLocal = setResult.Tables[0];
                if (tbLocal.Rows.Count > 0)
                {
                    flgExist = true;
                    dwLocal = tbLocal.Rows[0];
                }
                else
                {
                    dwLocal = tbLocal.NewRow();
                }
            }
            MapCBOOK(ref dwLocal, dwData);

            if (!flgExist)
            {

                InsertCBOOK(dwLocal, ITXDBVERSION);
            }
            UpdateCBOOK(dwLocal);
        }
        public void InsertCBOOK(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            switch (ITXDBVERSION)
            {
                case "YGR":
                    or1.CommandText = "INSERT INTO CBOOK(VAKBMANDANT, VAKBKOLL, VAKBART, VAKBKUNDNR, VAKBFARBE, VAKBKDARTNR, VAKBKDARTBEZ, VAKBKDFARBNR, VAKBANDRUCK, VAKBMEGEW, VAKBMENGEH, VAKBGEWVON, VAKBGEWBIS, VAKBGEWCA, VAKBGEWFKT, VAKBGEWGRP, VAKBMEGFKT, VAKBMEGGRP, VAKBAENDZL, VAKBAENDBEN, VAKBAENDDAT, VAKBNEUBEN, VAKBNEUDAT, VAKBFILLER)"
                        + " VALUES (:VAKBMANDANT, :VAKBKOLL, :VAKBART, :VAKBKUNDNR, :VAKBFARBE, '                         ', '                         ', '                             ', ' ', '   ', '   ', 0, 0, ' ', '000000000000', ' ', '000000000000', ' ', 0, '        ', '000000', '        ', '      ', '                                          ')";
                    or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                //VAFMANDANT VAFKOLL VAFART VAFFARBE VAFBEZ VAFFFARBE

            }
            
            Connect2Proxy(or1);
        }
        public DataSet QueryCBOOK(DataRow dwData, int option = 1)
        {
            DataSet Return;
            DataSet Result = new DataSet();
            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 0:
                    or1.CommandText = "SELECT * FROM CBOOK WHERE  ROWNUM<2 ";
                    break;
              
                case 99:
                    or1.CommandText = "SELECT * FROM CBOOK  WHERE ROWNUM<50";
                    break;
                default:
                    break;
            }
            Result = Connect2Proxy(or1);
            if (Result.Tables.Count > 0)
                Result.Tables[0].TableName = "CBOOK";
            Return = Result;
            return Return;
        }
        public void DeleteCBOOK(DataRow dwData, int option = 1)
        {

            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    or1.CommandText = "DELETE FROM CBOOK   WHERE  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                     or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                default:
                    break;
            }
            Connect2Proxy(or1);
        } 
      
        public void UpdateCBOOK(DataRow dwData, string ITXDBVERSION = "50.24", int option = 1)
        {
            OracleCommand or1;
            DataSet Result = new DataSet();
            or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    #region====Command==== 
                    or1.CommandText = "UPDATE CBOOK SET "
                    +"CBMANDANT= :CBMANDANT,"
                    +"CBOOKNO= :CBOOKNO,"
                    +"CBSTA= :CBSTA,"
                    +"CBDATE= :CBDATE,"
                    +"CBZOLLST= :CBZOLLST,"
                    +"CBDES1= :CBDES1,"
                    +"CBDES2= :CBDES2,"
                    +"CBVALFRM= :CBVALFRM,"
                    +"CBVALUPT= :CBVALUPT,"
                    +"CBLO= :CBLO,"
                    +"CBLB= :CBLB,"
                    +"CBBUCHKRS= :CBBUCHKRS,"
                    +"CBVB= :CBVB,"
                    +"CBQTY= :CBQTY,"
                    +"CBME= :CBME,"
                    +"CBMAXVAL= :CBMAXVAL,"
                    +"CBDAYSEXP= :CBDAYSEXP,"
                    +"CBDAYSCP= :CBDAYSCP,"
                    +"CBWASTE= :CBWASTE,"
                    +"CBWASTEREA= :CBWASTEREA,"
                    +"CBWASTEADD= :CBWASTEADD,"
                    +"CBTRBUCH= :CBTRBUCH,"
                    +"CBPERLOT= :CBPERLOT,"
                    +"CBPERNVE= :CBPERNVE,"
                    +"CBEXCRATE1= :CBEXCRATE1,"
                    +"CBEXCRATEID= :CBEXCRATEID,"
                    +"CBFILLER= :CBFILLER,"
                    +"CBAENZL= :CBAENZL,"
                    +"CBAENB= :CBAENB,"
                    +"CBAEND= :CBAEND,"
                    +"CBNEUB= :CBNEUB,"
                    +"CBNEUD= :CBNEUD,"
                        + " WHERE  "
                        + "  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                    #endregion

                    #region====Parameters====
          or1.Parameters.Add("CBMANDANT", OracleType.NVarChar).Value = dwData["CBMANDANT"].ToString(); 
          or1.Parameters.Add("CBOOKNO", OracleType.NVarChar).Value = dwData["CBOOKNO"].ToString(); 
          or1.Parameters.Add("CBSTA", OracleType.NVarChar).Value = dwData["CBSTA"].ToString(); 
          or1.Parameters.Add("CBDATE", OracleType.NVarChar).Value = dwData["CBDATE"].ToString(); 
          or1.Parameters.Add("CBZOLLST", OracleType.NVarChar).Value = dwData["CBZOLLST"].ToString(); 
          or1.Parameters.Add("CBDES1", OracleType.NVarChar).Value = dwData["CBDES1"].ToString(); 
          or1.Parameters.Add("CBDES2", OracleType.NVarChar).Value = dwData["CBDES2"].ToString(); 
          or1.Parameters.Add("CBVALFRM", OracleType.NVarChar).Value = dwData["CBVALFRM"].ToString(); 
          or1.Parameters.Add("CBVALUPT", OracleType.NVarChar).Value = dwData["CBVALUPT"].ToString(); 
          or1.Parameters.Add("CBLO", OracleType.NVarChar).Value = dwData["CBLO"].ToString(); 
          or1.Parameters.Add("CBLB", OracleType.NVarChar).Value = dwData["CBLB"].ToString(); 
          or1.Parameters.Add("CBBUCHKRS", OracleType.NVarChar).Value = dwData["CBBUCHKRS"].ToString(); 
          or1.Parameters.Add("CBVB", OracleType.NVarChar).Value = dwData["CBVB"].ToString(); 
          or1.Parameters.Add("CBQTY", OracleType.NVarChar).Value = dwData["CBQTY"].ToString(); 
          or1.Parameters.Add("CBME", OracleType.NVarChar).Value = dwData["CBME"].ToString(); 
          or1.Parameters.Add("CBMAXVAL", OracleType.NVarChar).Value = dwData["CBMAXVAL"].ToString(); 
          or1.Parameters.Add("CBDAYSEXP", OracleType.NVarChar).Value = dwData["CBDAYSEXP"].ToString(); 
          or1.Parameters.Add("CBDAYSCP", OracleType.NVarChar).Value = dwData["CBDAYSCP"].ToString(); 
          or1.Parameters.Add("CBWASTE", OracleType.NVarChar).Value = dwData["CBWASTE"].ToString(); 
          or1.Parameters.Add("CBWASTEREA", OracleType.NVarChar).Value = dwData["CBWASTEREA"].ToString(); 
          or1.Parameters.Add("CBWASTEADD", OracleType.NVarChar).Value = dwData["CBWASTEADD"].ToString(); 
          or1.Parameters.Add("CBTRBUCH", OracleType.NVarChar).Value = dwData["CBTRBUCH"].ToString(); 
          or1.Parameters.Add("CBPERLOT", OracleType.NVarChar).Value = dwData["CBPERLOT"].ToString(); 
          or1.Parameters.Add("CBPERNVE", OracleType.NVarChar).Value = dwData["CBPERNVE"].ToString(); 
          or1.Parameters.Add("CBEXCRATE1", OracleType.NVarChar).Value = dwData["CBEXCRATE1"].ToString(); 
          or1.Parameters.Add("CBEXCRATEID", OracleType.NVarChar).Value = dwData["CBEXCRATEID"].ToString(); 
          or1.Parameters.Add("CBFILLER", OracleType.NVarChar).Value = dwData["CBFILLER"].ToString(); 
          or1.Parameters.Add("CBAENZL", OracleType.NVarChar).Value = dwData["CBAENZL"].ToString(); 
          or1.Parameters.Add("CBAENB", OracleType.NVarChar).Value = dwData["CBAENB"].ToString(); 
          or1.Parameters.Add("CBAEND", OracleType.NVarChar).Value = dwData["CBAEND"].ToString(); 
          or1.Parameters.Add("CBNEUB", OracleType.NVarChar).Value = dwData["CBNEUB"].ToString(); 
          or1.Parameters.Add("CBNEUD", OracleType.NVarChar).Value = dwData["CBNEUD"].ToString(); 
                    #endregion
                    break; 
                default:
                    break;
            }


            Connect2Proxy(or1);
        }
    }
}
